package com.cictec.middleware.tde.rm.disp.receiver.handler;

import java.util.Date;

import org.apache.mina.core.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.cictec.middleware.commons.utils.DateUtils;
import com.cictec.middleware.commons.utils.PropertiesUtils;
import com.cictec.middleware.core.pojo.MiddlewarePosition;
import com.cictec.middleware.feign.distribute.MiddlewarePositionService;
import com.cictec.middleware.tde.rm.disp.annotation.HandleMessages;
import com.cictec.middleware.tde.rm.disp.receiver.AbstractReceiverHandler;
import com.cictec.middleware.tde.rm.protocol.TerminalMessage;
import com.cictec.middleware.tde.rm.protocol.rm.message.Rm0x03TerminalMessage;

/**
 * 0X03上传进出定点数据，接收指令应答确认【已在AutoResponseFilter中配置回复应答】
 * 
 * @author Administrator
 *
 */
@Component
@HandleMessages(0X03)
public class TerminalEnterOutPositionHandler extends AbstractReceiverHandler {

	private static Logger logger = LoggerFactory.getLogger(TerminalEnterOutPositionHandler.class);

	@Autowired
	private MiddlewarePositionService middlewarePositionService;

	@Override
	public void handleMessage(TerminalMessage message, IoSession session) {
		
		Rm0x03TerminalMessage msg = (Rm0x03TerminalMessage) message;
		
		/* ************* 测试代码 start ********************/
		boolean flag = PropertiesUtils.getBoolean("isTest", false);
		if(true){
			test(msg);
		}
		/* ************* 测试代码 end ********************/
		
//		logger.debug("【0X03上传进出定点数据】终端 {} Session {}  createTime:{}  sendTime:{}", msg.getTerminalId(), msg.getSessionId(), DateUtils.formatDateTime(teop.getCreateTime()), DateUtils.formatDateTime(teop.getHeader().getSendTime()));
		
		switch (msg.getPositionType()) {
		case 0x01: {
			// 公交站点
			enterOutStation(msg);
			break;
		}
		case 0x02: {
			// 拐弯点（进出标识都为0）

			break;
		}
		case 0x03: {
			// 安全事故黑点（进出标识都为0）s

			break;
		}
		case 0x04: {
			// 标杆

			break;
		}
		}
	}

	
	/**
	 * 处理车辆进出站
	 * 
	 * @param msg
	 */
	private void enterOutStation(Rm0x03TerminalMessage msg) {
		
//		Date sentTime = tp.getHeader().getSendTime();
//		String logPath = MultiLogFileNameUtils.getPositionLogName(null, tp.getTerminalId(), DateUtils.formatDate(sentTime));
//		MultiLog log = MultiLogUtils.getMultiLog(logPath);
//		log.debug("lineId:{}, deviceCode:{}, seq:{} enterOut:{} createTime:{}  sendTime:{}"
//				, tp.getHeader().getLineId()
//				, tp.getTerminalId()
//				, tp.getPositionNumber()
//				, tp.getEnterOutStatus()
//				, DateUtils.formatDateTime(tp.getCreateTime())
//				, DateUtils.formatDateTime(sentTime)
//				);
		
		logger.debug("【0X03上传进出定点数据】【进出站信息】终端 {} Session {}  seq:{} enterOut:{} createTime:{}  sendTime:{}"
				, msg.getTerminalId()
				, msg.getSessionId()
				, msg.getPositionNumber()
				, msg.getEnterOutStatus()
				, DateUtils.formatDateTime(msg.getCreateTime())
				, DateUtils.formatDateTime(msg.getHeader().getSendTime()));
		
		
		MiddlewarePosition position = new MiddlewarePosition(msg.getUuid());
		position.setLineId(msg.getHeader().getLineId());
		position.setDevCode(msg.getHeader().getTerminalId()+"");
		position.setCreateTime(msg.getCreateTime());
		position.setSendTime(msg.getHeader().getSendTime());
		position.setLat(msg.getLat());
		position.setLng(msg.getLng());
		position.setAngle(msg.getAngle());
		position.setSpeed(msg.getSpeed());
		
		position.setPositionNumber(msg.getPositionNumber());
		position.setEnterOutStatus(msg.getEnterOutStatus() == 0 ? 1 : 2);
		position.setTriggerType(msg.getTriggerType());
		
		// 中间件位置信息处理入口
		middlewarePositionService.positionUpload(position);
		
	}
	
	private void test(Rm0x03TerminalMessage msg){
		Date sendTime = msg.getHeader().getSendTime();
		Date createTime = msg.getCreateTime();
		String strSendDate = DateUtils.formatDateTimes(sendTime);
		String strCreateDate = DateUtils.formatDateTimes(createTime);
		
		String nowDate=  DateUtils.getDate();
//		nowDate = "2017-06-07";
		strSendDate = nowDate + strSendDate.substring(10);
		strCreateDate = nowDate + strCreateDate.substring(10);
		
		msg.getHeader().setSendTime(DateUtils.parseDateTimes(strSendDate));
		msg.setCreateTime(DateUtils.parseDateTimes(strCreateDate));
//		msg.getHeader().setSendTime(new Date());
//		msg.setCreateTime(new Date());
		
		
//		if(TerminalPositionHandler.testDateTime.get(tp.getTerminalId()) == null){
//			TerminalPositionHandler.testDateTime.put(tp.getTerminalId(), new Date());
////			TerminalPositionHandler.testDateTime.put(tp.getTerminalId(), DateUtils.parseDateTime("2017-07-04 08:30:00"));
//		}else{
//			TerminalPositionHandler.testDateTime.put(tp.getTerminalId(), DateUtils.getDateFromSeconds(TerminalPositionHandler.testDateTime.get(tp.getTerminalId()), 6));
//		}
//		tp.getHeader().setSendTime(TerminalPositionHandler.testDateTime.get(tp.getTerminalId()));
//		tp.setCreateTime(TerminalPositionHandler.testDateTime.get(tp.getTerminalId()));
	}

}
